package com.losl.framework.web.service;

import com.losl.common.core.domain.entity.SysUser;
import com.losl.common.core.domain.model.LoginUser;
import com.losl.common.util.StringUtils;
import com.losl.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 用户登陆验证
 * @author Maxwell
 */
@Component
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userService.selectUserByName(username);
        if (StringUtils.isNull(sysUser)){
            throw new UsernameNotFoundException("用户"+username+"不存在");
        }else if(sysUser.getStatus().equals("1")){
            throw new UsernameNotFoundException("您的账户出现异常，已被停用");
        }
        return new LoginUser(sysUser);
    }

}
